﻿using System;
using System.Linq;
using Pap.Data;
using Pap.Data.Abstraction.Utils;

namespace Pap.Services.Customer
{
    /// <summary>
    /// 
    /// </summary>
    public class OverAge : CustomerValidatorBase
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository administrationRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="OverAge"/> class.
        /// </summary>
        /// <param name="_db">The _DB.</param>
        /// <param name="administrationRepository">The administration repository.</param>
        public OverAge(IDatabase _db, IAdministrationRepository administrationRepository)
            : base(_db)
        {
            isError = false;
            this.administrationRepository = administrationRepository;
            canValidateByName = false;
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, Pap.Data.ICustomer customer)
        {
            Guid lastStageTakenID = (from loan in _database.CustomerLoans
                                     join loanHistory in _database.CustomerLoanHistories
                                     on loan.LoanID equals loanHistory.LoanID
                                     where loan.CustomerID == customer.CustomerID
                                     orderby loanHistory.LoanCloseDate descending
                                     select loan.ProjectStageID).FirstOrDefault();

            IProjectStage lastStageTaken = _database.ProjectStages.SingleOrDefault(p => p.ProjectStageID == lastStageTakenID);
            IProjectStage nextStage = (lastStageTaken == null) ? null : administrationRepository.GetNextStage(lastStageTaken);
            if (nextStage == null)
            {
                return false;
            }
            else
            {
                int maxAge = nextStage.AgeLimit;
                //TODO: Age <= or just <

                TimeSpan customerAge = DateTime.Now.Subtract(customer.Birthdate);
                int ageYears = (int)(customerAge.TotalDays / 365.25);

                if (ageYears <= (int)maxAge)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customerName">Name of the customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, string customerName)
        {
            throw new Exception("Unsupported. Can't validate for open loan by name only.");
        }
    }
}
